package com.ldx.blog.common.interceptor;

import com.ldx.blog.common.constant.Constants;
import com.ldx.blog.common.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(Constants.USER_TOKEN_HEADER_KEY);
        Claims claims = JwtUtils.parseToken(token);
        log.info("从header中获得token:"+token);
        if(token == null){
            response.setStatus(401);
            return false;
        }
        if(claims == null){
            response.setStatus(401);
            return false;
        }
        return true;
    }

}
